//
//  CateListViewController.m
//  NewIMGII
//
//  Created by 步遴 李 on 12-6-28.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CateListViewController.h"
#import "blogViewController.h"
@implementation CateListViewController
@synthesize catTitle,blogs,blog;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {


    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    pre = [NSPredicate predicateWithFormat:@"(category like %@)",self.catTitle];
    appdelegate = [[UIApplication sharedApplication]delegate];
    context = [appdelegate managedObjectContext];
    entity =[NSEntityDescription entityForName:@"Blog" inManagedObjectContext:context];
    cdrequest = [[NSFetchRequest alloc]init];
    [cdrequest setPredicate:pre];
    [cdrequest setEntity:entity];
    self.blogs = [context executeFetchRequest:cdrequest error:&error];
    [cdrequest release];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark-
#pragma mark Table View Delegate Methods

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.blog = [self.blogs objectAtIndex:[indexPath row]];
    blogViewController *blogviewController = [[blogViewController alloc]initWithNibName:@"blogViewController" bundle:nil];
    blogviewController.blog = [self.blogs objectAtIndex:[indexPath row]];
    [self.navigationController pushViewController:blogviewController animated:YES];
    [blogviewController release];
}
#pragma mark-
#pragma mark Table View Data Source Methods 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.blogs count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleIdent = @"simpleIdent";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdent];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleIdent]autorelease];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
    }

    self.blog = [self.blogs objectAtIndex:[indexPath row]];
    cell.textLabel.text = self.blog.title;
    cell.detailTextLabel.text = self.blog.context;
    
    return cell;
}

@end
